<?php
declare (strict_types=1);

namespace app\index\controller;

use app\common\model\Assets;
use app\common\model\Bank;
use app\common\model\Place;
use app\common\model\Relation;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\facade\View;
use app\common\model\User as UserModel;

class User extends Base
{

    public function index()
    {
        View::assign('assets', Assets::getFields(3));
        return View::fetch();
    }

    public function info()
    {
        return View::fetch();
    }

    public function setting()
    {
        return View::fetch();
    }

    public function contact()
    {
        return View::fetch();
    }

    public function query()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            return json(Place::query_code($this->user->id,$param['position']));
        }
        $position = UserModel::where(['place_id'=>$this->user->id,'delete_time'=>0])->order('position','asc')->column('position');
        View::assign([
            'position' => $position
        ]);
        return View::fetch();
    }

    public function team()
    {
        $team_num = Relation::where(['uid' => $this->user->id, 'algebra' => 1])->count();
        $dpn_num = Place::where(['uid' => $this->user->id])->count();
        View::assign([
            'team_num' => $team_num,
            'dpn_num' => $dpn_num
        ]);
        return View::fetch();
    }

    public function my_place()
    {
        if ($this->request->isAjax()) {
            $page = $this->request->param('page', 1);
            $map = ['uid' => $this->user->id, 'layers' => 1];
            return json(Place::getPageList($map, '*', $page, 20, false, ['fresh']));
        }
        return View::fetch();
    }

    public function my_team()
    {
        if ($this->request->isAjax()) {
            $page = $this->request->param('page', 1);
            $map = ['uid' => $this->user->id, 'algebra' => 1];
            return json(Relation::getPageList($map, '*', $page, 20, false, ['fresh']));
        }
        return View::fetch();
    }

    public function methodList()
    {
        return View::fetch();
    }

    public function method($type)
    {
        $mode = ['alipay' => '支付宝', 'wechat' => '微信', 'bank' => '银行卡'];
        View::assign([
            'type'      => $type,
            'title'     => $mode[$type],
            'bank_list' => Bank::where(['status' => 1, 'delete_time' => 0])->column('title', 'id')
        ]);
        return View::fetch();
    }

    public function method_save($type)
    {
        try {
            $param = $this->request->post(['name', 'image', 'bank_name', 'bank_open', 'bank_account_name', 'bank_number']);

            $v = $type == 'bank' ? 'MethodBank' : 'MethodImg';
            try {
                $this->validate($param, $v);
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }
            switch ($type) {
                case "bank":
                    break;
                default:
                    $param[$type . '_name'] = $param['name'];
                    $param[$type . '_image'] = $param['image'];
                    unset($param['name']);
                    unset($param['image']);
            }

            UserModel::update($param, ['id' => $this->user->id]);

            return json(['code' => 1, 'msg' => '已保存']);

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public function edit()
    {
        try {
            $param = $this->request->post(['nickname', 'photo']);
            UserModel::update($param, ['id' => $this->user->id]);
            return Result::success('已修改');
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }

    }

    public function logout()
    {
        $token = md5(time() . $this->user['id'] . $this->request->ip());

        UserModel::update([
            'token' => $token
        ], ['id' => $this->user['id']]);
        cookie('token', null);

        return Result::fastReturn(['code' => 1, 'msg' => '已退出登录', 'url' => url('login/index')->build()]);
    }

    public function save_share()
    {
        $param = $this->request->param();
        $place = UserModel::where(['code' => $param['shar_place'], 'is_active' => 2])->find();
        if (empty($place)) {
            return ['code' => -1, 'msg' => '接点人不存在或未激活'];
        }
        UserModel::update(['shar_place'=>$param['shar_place']],['id'=>$this->user->id]);
        $user = UserModel::where(['id'=>$this->user->id])->find();
        $url = "http://" . $_SERVER['SERVER_NAME'] . "/index/register/index.html?code=" . $user->code.'&&place_code='.$user->shar_place;
        return Result::fastReturn(['code' => 1, 'msg' => '修改成功', 'url' => $url]);
    }

    public function share()
    {
//        $url = "http://" . $_SERVER['SERVER_NAME'] . "/index/register/index.html?code=" . $this->user->code.'&&place_code='.$this->user->shar_place;
//        View::assign('url', $url);
        return View::fetch();
    }

    public function release()
    {
        if ($this->request->isAjax()) {
            return Result::fastReturn(Relation::getPageList(['uid' => $this->user->id], '*', $this->getPage(), 20, false));
        }
        return View::fetch();
    }

}

